import request from './index.js'
import { API_CONFIG } from '../config/api.js'

// 农场用户相关API
export const userApi = {
    // 获取用户信息 GET /user
    getUserInfo() {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.USER_INFO,
            method: 'GET'
        })
    },

    // 登录 POST /user/login { address, sign }
    login(data) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.LOGIN,
            method: 'POST',
            data
        })
    },

    // 退出登录
    logout() {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.LOGOUT,
            method: 'POST'
        })
    },

    // 填写邀请码 POST /user/invitation_code { invitationCode }
    submitInvitationCode(data) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.INVITATION_CODE,
            method: 'POST',
            data
        })
    },

    // 上传头像 POST /user/upload_avatar multipart
    uploadAvatar(filePath) {
        // 这里使用 uni.uploadFile 以适配文件上传
        return new Promise((resolve, reject) => {
            const token = uni.getStorageSync('x-token') || ''
                // 显示加载
            uni.$emit('show-loading')
            uni.uploadFile({
                url: API_CONFIG.BASE_URL + API_CONFIG.ENDPOINTS.FARM.UPLOAD_AVATAR,
                filePath,
                name: 'file',
                header: {
                    'x-token': token
                },
                success: (res) => {
                    try {
                        const data = JSON.parse(res.data)
                        resolve(data)
                    } catch (e) {
                        reject(e)
                    }
                },
                fail: reject,
                complete: () => {
                    // 关闭加载
                    uni.$emit('hide-loading')
                }
            })
        })
    },

    // 修改昵称 POST /user/nick_name { userId, nickName }
    updateNickName(data) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.NICK_NAME,
            method: 'POST',
            data
        })
    },

    // 获取头像地址 GET /user/get_avatar/{filename}
    getAvatar(filename) {
        return request({
            url: `${API_CONFIG.ENDPOINTS.FARM.GET_AVATAR}?resource=${filename}`,
            method: 'GET'
        })
    },

    // 获取新手礼包列表
    getNewbieGift() {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.NEWBIE_GIFT,
            method: 'GET'
        })
    },

    // 领取新手礼包
    receiveNewbieGift(id) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.RECEIVE_NEWBIE_GIFT + id,
            method: 'POST'
        })
    },

    // 填写邀请码 POST /user/invitation_code { invitationCode }
    submitInvitationCode(data) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.INVITATION_CODE,
            method: 'POST',
            data
        })
    },

    // 获取好友列表
    getFriendList() {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.FRIEND_LIST,
            method: 'GET'
        })
    },

    // 获取好友土地列表
    getFriendLandList(id) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.FRIEND_LAND_LIST + id,
            method: 'GET'
        })
    },

    // 生成充值订单 POST /user/order-deposit { amount }
    createDepositOrder(data) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.ORDER_DEPOSIT,
            method: 'POST',
            data
        })
    },

    // 生成提现订单 POST /user/order-withdraw { applyAmount }
    createWithdrawOrder(data) {
        return request({
            url: API_CONFIG.ENDPOINTS.FARM.ORDER_WITHDRAW,
            method: 'POST',
            data
        })
    },

}